﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MesTool
{
    public class OperationBLL
    {
        static string operationcheckPostURL = "http://192.168.2.23:9213/integrate/dataCollector/jiamiprocessInspec";
        static string checkdataPostURL = "http://192.168.2.23:9213/integrate/dataCollector/testRecord";
        static string barcodestatusPostURL = "http://192.168.2.23:9213/integrate/dataCollector/updateSNStatus";
        private static void GetIP(string ipstr)
        {
            IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
            ipaddress = ipHost.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork && x.ToString().Contains(ipstr));
        }
        static IPAddress ipaddress = null;
        /// <summary>
        /// 工序校验接口
        /// </summary>
        /// <param name="serial">sn</param>
        /// <param name="operation">工序</param>
        /// <param name="errMsg">错误信息</param>
        /// <param name="mocode">订单号</param>
        /// <returns></returns>
        public static bool OperationCheck(string serial, string operation, ref string errMsg, ref string mocode, string ip = "")
        {
            try
            {
                if (ip != "")
                {
                    GetIP(ip);
                }
                Model.HttpModel.OperationcheckDto postModel = new Model.HttpModel.OperationcheckDto()
                {
                    operation_name = operation,
                    serial_number = serial,
                };
               string result = HttpHelper.Post(operationcheckPostURL, JsonConvert.SerializeObject(postModel), ipaddress);
                Model.HttpModel.OperationcheckRb resultModel = JsonConvert.DeserializeObject<Model.HttpModel.OperationcheckRb>(result);
                if (resultModel.code != 200)
                {
                    errMsg = resultModel.message;
                    return false;
                }
                if (resultModel.result != 1)
                {
                    errMsg = resultModel.message;
                    return false;
                }
                if (resultModel.message != "success")
                {
                    errMsg = resultModel.message;
                    return false;
                }
                mocode = resultModel.mocode;
                return true;
            }
            catch (Exception ex)
            {
                errMsg = "工序校验接口调用出错";
                return false;
            }
        }
        /// <summary>
        /// 过站记录保存
        /// </summary>
        /// <param name="postModel">dto</param>
        /// <param name="errMsg">错误信息</param>
        /// <returns></returns>
        public static bool PostCheckdata(Model.HttpModel.CheckdataDto postModel, ref string errMsg)
        {
            try
            {
                Model.HttpModel.BarcodestatusDto postModel1 = new Model.HttpModel.BarcodestatusDto();
                postModel1.serial_number = postModel.serial_number;
                postModel1.operation_name=postModel.operation_name;
                postModel1.quality=postModel.quality;
                if (PostBarcodestatus(postModel1, ref errMsg) == false)
                {
                    return false;
                }
                string result = HttpHelper.Post(checkdataPostURL, JsonConvert.SerializeObject(postModel), ipaddress);
                Model.HttpModel.CheckdataRb resultModel = JsonConvert.DeserializeObject<Model.HttpModel.CheckdataRb>(result);
                if (resultModel.code != 200)
                {
                    errMsg = resultModel.message;
                    return false;
                }
                if (resultModel.result != 1)
                {
                    errMsg = resultModel.message;
                    return false;
                }
                if (resultModel.message != "success")
                {
                    errMsg = resultModel.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                errMsg = "过站记录保存接口调用出错";
                return false;
            }

        }
        /// <summary>
        /// 更新序列号状态
        /// </summary>
        /// <param name="postModel">dto</param>
        /// <param name="errMsg">错误信息</param>
        /// <returns></returns>
        public static bool PostBarcodestatus(Model.HttpModel.BarcodestatusDto postModel, ref string errMsg)
        {
            try
            {
                string result = HttpHelper.Post(barcodestatusPostURL, JsonConvert.SerializeObject(postModel), ipaddress);
                Model.HttpModel.BarcodestatusRb resultModel = JsonConvert.DeserializeObject<Model.HttpModel.BarcodestatusRb>(result);
                if (resultModel.code != 200)
                {
                    errMsg = resultModel.message;
                    return false;
                }
                if (resultModel.result != 1)
                {
                    errMsg = resultModel.message;
                    return false;
                }
                if (resultModel.message != "success")
                {
                    errMsg = resultModel.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                errMsg = "更新序列号状态接口调用出错";
                return false;
            }
        }
    }
}
